{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "422ac7c9-201c-4665-ab1c-65e79ee2cd56",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 1.双色LED灯实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：1.双色LED灯实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：双色LED灯实验\n",
    "双色LED灯控制程序，通过PWM方式控制LED的亮度！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f58331-df2f-409c-b08b-1401a5100a80",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "24bb5df6-9701-49a1-8df4-dcbd1a87c5fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpiozero import PWMLED\n",
    "from time import sleep"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf81ed90-ea51-4447-b522-85494d089b54",
   "metadata": {},
   "source": [
    "## 2.定义颜色范围值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "20561d89-e334-41e0-9358-07f9b385ffdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = [0xFF00, 0x00FF, 0x0FF0, 0xF00F]\n",
    "makerobo_pins = (17, 18)  # PIN管脚字典，红色LED接GPIO17，绿色LED接GPIO18"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90322f04-34c2-448f-8ec0-481b8b435ef1",
   "metadata": {},
   "source": [
    "## 3.实例化LED管脚"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6167bf43-79f6-4e37-9b34-2cd241415a95",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_R = PWMLED(pin=makerobo_pins[0],initial_value = 0, frequency=2000)  # 设置频率为2KHz\n",
    "p_G = PWMLED(pin=makerobo_pins[1],initial_value = 0, frequency=2000)  # 设置频率为2KHz"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b01200-b8f4-4906-9fef-ee5921375822",
   "metadata": {},
   "source": [
    "## 4.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "45e18609-d482-4008-9b7f-64ee3599f522",
   "metadata": {},
   "outputs": [],
   "source": [
    "def makerobo_pwm_map(x, in_min, in_max, out_min, out_max):\n",
    "\treturn (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min\n",
    "\n",
    "def makerobo_set_Color(col):   # 例如:col = 0x1122\n",
    "\tR_val = col  >> 8\n",
    "\tG_val = col & 0x00FF\n",
    "\t# 把0-255的范围同比例缩小到0-100之间\n",
    "\tR_val = makerobo_pwm_map(R_val, 0, 255, 0, 100)\n",
    "\tG_val = makerobo_pwm_map(G_val, 0, 255, 0, 100)\n",
    "\t\n",
    "\tp_R.value = (R_val)/100.0     # 改变占空比\n",
    "\tp_G.value = (G_val)/100.0     # 改变占空比\n",
    "\n",
    "# 调用循环函数\n",
    "def makerobo_loop():\n",
    "\twhile True:\n",
    "\t\tfor col in colors:\n",
    "\t\t\tmakerobo_set_Color(col)\n",
    "\t\t\tsleep(0.5)\n",
    "# 释放资源\n",
    "def makerobo_destroy():\n",
    "\tp_G.close()\n",
    "\tp_R.close()\n",
    "\n",
    "# 程序入口\n",
    "if __name__ == \"__main__\":\n",
    "\ttry:\n",
    "\t\tmakerobo_loop()        # 调用循环函数\n",
    "\texcept KeyboardInterrupt:  # 当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tmakerobo_destroy()     # 释放资源"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
